package com.cjc.hotel.server.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjc.hotel.server.entity.TAdmin;
import com.cjc.hotel.server.entity.TRoomType;
import com.cjc.hotel.server.handler.CommonException;
import com.cjc.hotel.server.result.Result;
import com.cjc.hotel.server.service.TAdminService;
import com.cjc.hotel.server.service.TRoomService;
import com.cjc.hotel.server.service.TRoomTypeService;
import com.cjc.hotel.server.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 房间类型控制器
 * @author chenjuncai
 */
@Controller
@RequestMapping("/admin/roomType")
public class RoomTypeController {
    @Autowired
    private TRoomTypeService roomTypeService;

    /**
     * 去列表页面
     * @return 结果
     */
    @GetMapping("")
    public String toList(){
        return "roomType/roomType";
    }
    /**
     * 去添加页面
     * @return 结果
     */
    @GetMapping("toAdd")
    public String toAdd(){
        return "roomType/add";
    }
    /**
     * 修改页面
     * @param roomTypeId
     * @param model
     * @return
     */
    @GetMapping("toEdit/{roomTypeId}")
    public String toEdit(@PathVariable("roomTypeId") Integer roomTypeId, Model model)
    {
        TRoomType byId = roomTypeService.getById(roomTypeId);
        model.addAttribute("roomType",byId);
        return "roomType/edit";
    }
    /**
     * 保存房间类型
     * @param roomType 房间类型
     * @return String
     */
    @ApiOperation(value = "保存房间类型员")
    @ResponseBody
    @PostMapping("/saveOrUpdate")
    public Result<String> saveOrUpdate(@Validated  TRoomType roomType){
        // 判断用户名是否重复
        QueryWrapper<TRoomType> roomTypeQueryWrapper = new QueryWrapper<>();
        roomTypeQueryWrapper.eq("room_type_name",roomType.getRoomTypeName());
        int count = roomTypeService.count(roomTypeQueryWrapper);
        if(count>0&&roomType.getRoomTypeId()==null){
            throw new CommonException("用户名重复！");
        }
        // 插入数据库
        boolean b = roomTypeService.saveOrUpdate(roomType);
        return b? ResultUtil.success(""):ResultUtil.fail("操作失败");
    }

    /**
     *  删除房间类型
     * @param roomTypeId 班别编号
     * @return String
     */
    @ApiOperation(value = "删除房间类型员")
    @ResponseBody
    @DeleteMapping("/remove/{roomTypeId}")
    public Result<String> remove(@PathVariable("roomTypeId") @ApiParam(name = "编号") String roomTypeId){

        // 班别编号是否为空或空字符串
        if(null == roomTypeId || "".equals(roomTypeId)){
            throw new CommonException("房间类型员Id不存在！");
        }
        // 房间类型所属学生是否有
        QueryWrapper<TRoomType> roomTypeQueryWrapper = new QueryWrapper<>();
        roomTypeQueryWrapper.eq("room_type_id",roomTypeId);
        int count = roomTypeService.count(roomTypeQueryWrapper);
        if(count==0){
            throw new CommonException("房间类型员不存在！");
        }
        // 删除
        boolean b = roomTypeService.removeById(roomTypeId);
        return b? ResultUtil.success(""):ResultUtil.fail("操作失败");
    }

    /**
     *  房间类型员列表
     * @param limit 页面记录数
     * @param page 当前页面
     * @return String
     */
    @ApiOperation(value = "房间类型员列表")
    @ResponseBody
    @GetMapping("/list")
    public Result<List<TRoomType>> list(@ApiParam(name = "page",value = "页面") @RequestParam Long page,
                                     @ApiParam(name = "limit",value ="页面记录数" ) @RequestParam Long limit,
                                     @ApiParam(name = "keyword",value ="关键字" ) @RequestParam(required = false) String keyword){
        QueryWrapper<TRoomType> roomTypeQueryWrapper = null;
        if(keyword!=null){
            roomTypeQueryWrapper = new QueryWrapper<>();
            roomTypeQueryWrapper.like("username",keyword);
        }

        IPage<TRoomType> roomTypeIPage = new Page<>(page,limit);
        return ResultUtil.defineSuccess(0,roomTypeService.page(roomTypeIPage,roomTypeQueryWrapper).getRecords());
    }
}
